package com.atguigu.tingshu.search.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * projectName: com.atguigu.tingshu.search.config
 *
 * @author: 吴振鹏
 * description:
 * time: 2025/9/4 19:28
 */
@Component
public class ThreadPoolExecutorConfig {

    //自定义线程池
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {

        //获取当前CPU核数
        int cpuCoreNum = Runtime.getRuntime().availableProcessors();

        System.out.println("当前CPU核数：" + cpuCoreNum);

        return new ThreadPoolExecutor(
                cpuCoreNum,//核心线程数
                cpuCoreNum*2,//最大线程数
                3,//线程空闲时间
                TimeUnit.SECONDS,//时间单位
                new ArrayBlockingQueue<>(100),//阻塞队列
                Executors.defaultThreadFactory(),//线程工厂
                new ThreadPoolExecutor.AbortPolicy()//拒绝策略
        );

    }

}
